package com.pulaien.web.controller.customermessage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.R;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.system.domain.Customer;
import com.pulaien.system.domain.Notice;
import com.pulaien.system.domain.dto.ExtNotice;
import com.pulaien.system.domain.vo.NoticeVo;
import com.pulaien.system.mapper.CustomerMapper;
import com.pulaien.system.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yuanlai
 * @create 20230403 10:52
 **/

@RestController
@Api(value = "NoticeController", tags = "买家端-公告管理")
@RequestMapping("/shop/notice")
public class CustomerNoticeController extends BaseController {

    private final INoticeService noticeService;


    @Autowired
    private CustomerMapper customerMapper;

    public CustomerNoticeController(INoticeService noticeService) {
        this.noticeService = noticeService;
    }


    @ApiOperation(value = "公告列表分页展示(多语言)")
    @GetMapping(value = "/list")
    public TableDataInfo list(
            ExtNotice notice) {
        //String lang = request.getHeader(ConstantsUtil.HEAD_LANGUAGE);
        startPage();
        List<Notice> record = noticeService.selectNoticeList(notice);


        Long userId = SecurityUtils.getUserId();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , userId);
        Customer customer = customerMapper.selectOne(queryWrapper);
        customer.setNoticeSize(0);
        customerMapper.updateById(customer);

        return getDataTable(record);
    }


    @ApiModelProperty(value = "公告详情(多语言)")
    @GetMapping(value = "/info/{id}")
    public R info(
            @PathVariable("id") Long id,
            HttpServletRequest request
    ){
        String lang = request.getHeader(ConstantsUtil.HEAD_LANGUAGE);
        Notice noticeInfo = noticeService.selectNoticeById(id);
        NoticeVo vo = new NoticeVo();
        BeanUtils.copyProperties(noticeInfo,vo);

        //多语言处理
        if (StringUtils.isNotEmpty(lang)){
            switch (lang) {
                case ConstantsUtil.LANG_EN:
                    vo.setNoticeTitle(noticeInfo.getNoticeTitleEn());
                    vo.setNoticeContent(noticeInfo.getNoticeContentEn());
                    vo.setNoticeContentNoFormat(noticeInfo.getNoticeContentNoFormatEn());
                    break;
                case ConstantsUtil.LANG_JA:
                    vo.setNoticeTitle(noticeInfo.getNoticeTitleJa());
                    vo.setNoticeContent(noticeInfo.getNoticeContentJa());
                    vo.setNoticeContentNoFormat(noticeInfo.getNoticeContentNoFormatJa());
                    break;
                default:
                    vo.setNoticeTitle(noticeInfo.getNoticeTitleZh());
                    vo.setNoticeContent(noticeInfo.getNoticeContentZh());
                    vo.setNoticeContentNoFormat(noticeInfo.getNoticeContentNoFormatZh());
                    break;
            }
        }else {
            vo.setNoticeTitle(noticeInfo.getNoticeTitleZh());
            vo.setNoticeContent(noticeInfo.getNoticeContentZh());
            vo.setNoticeContentNoFormat(noticeInfo.getNoticeContentNoFormatZh());
        }
        return R.ok(vo);
    }


}
